//
//  YWTemTroubleDetailTableViewController.m
//  maintain
//
//  Created by Kyo on 16/8/18.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWTemTroubleDetailTableViewController.h"
#import "UIColor+CustomColors.h"
#import "YWCommonTool.h"
#import "YWConst.h"
#import "YWMacro.h"
#import "UITableView+FDTemplateLayoutCell.h"

#import "YWMission.h"
#import "YWTempTroubleFinishBillCell.h"
#import "YWMissionManager.h"
#import "YWEquipmentTroubleRecord.h"
#import "YWEquipmentFieldRecord.h"
#import "YWEquipmentType.h"
#import "YWReportTroubleModel.h"
#import "YWTroubleMissionModel.h"
#import "YWTroubleMissionManager.h"


@interface YWTemTroubleDetailTableViewController ()
@property (nonatomic, strong) UILabel *tableHeadLabel;
@property (nonatomic, copy) NSString *fieldRecordContent;
@end

@implementation YWTemTroubleDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"故障详情";
    self.view.backgroundColor = [UIColor customBackgroudColor];
    
    [self.tableView registerClass:[YWTempTroubleFinishBillCell class] forCellReuseIdentifier:YWTempTroubleFinishBillCellIdentifier];
    
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.tableHeaderView = self.tableHeadLabel;
    self.tableView.estimatedRowHeight = 44.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)fieldRecordContent{
    if (!_fieldRecordContent) {
        //设备参数
        
        YWEquipmentTroubleRecord* troubleRecord = nil;
        NSArray *fieldRecordArray = nil;
        if (self.reportTrouble){
            troubleRecord = [[YWTroubleMissionManager shareManager] troubleRecordWithMissionId:_reportTrouble.troubleId];
            fieldRecordArray = [[YWMissionManager shareManager] equipmentFieldRecordsWithUniqueTag:troubleRecord.uniqueTag maintenanceTypeId:YWMaintenanceTypeTrouble];
        }else if (self.troubleMissionModel){
            troubleRecord = [[YWTroubleMissionManager shareManager] troubleRecordWithMissionId:_troubleMissionModel.trouble.troubleId];
            fieldRecordArray = [[YWMissionManager shareManager] equipmentFieldRecordsWithUniqueTag:troubleRecord.uniqueTag maintenanceTypeId:YWMaintenanceTypeTrouble];
        }else if (self.troubleRecord){
            fieldRecordArray = self.troubleRecord.equipmentFieldRecord;
        }
        
        NSMutableString *contenStr = [NSMutableString string];
        if (fieldRecordArray.count) {
            __block NSString *str = nil;
            [fieldRecordArray enumerateObjectsUsingBlock:^(YWEquipmentFieldRecord*  _Nonnull fieldrecord, NSUInteger idx, BOOL * _Nonnull stop) {
                if (idx != 0) {
                    [contenStr appendString:@"\n"];
                }
                if (fieldrecord.fieldTypeId == YWEquipmentFieldTypeIdDownSelect) {
                    str = [NSString stringWithFormat:@"%@: %@",fieldrecord.fieldName,fieldrecord.fieldOption.fieldOptionText?:@"未填写"];
                }else{
                    str = [NSString stringWithFormat:@"%@: %@",fieldrecord.fieldName,fieldrecord.fieldValue?:@"未填写"];
                }
                [contenStr appendString:str];
            }];
        }else{
            [contenStr appendString:@"无"];
        }
        _fieldRecordContent = [NSString stringWithFormat:@"%@",contenStr];
    }
    return _fieldRecordContent;
}

-(UILabel *)tableHeadLabel{
    if (!_tableHeadLabel) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        label.font = KTitleFont;
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor whiteColor];
        if (self.reportTrouble) {
            label.attributedText = [YWCommonTool labelAttributedStringWithFristStr:@"编号: " secondStr:self.reportTrouble.troubleSequence secondColor:[UIColor customYellowColor]];
        }else if (self.troubleRecord){
            label.attributedText = [YWCommonTool labelAttributedStringWithFristStr:@"编号: " secondStr:self.troubleRecord.recordSequence secondColor:[UIColor customYellowColor]];
        }else{
            label.attributedText = [YWCommonTool labelAttributedStringWithFristStr:@"编号: " secondStr:@"无" secondColor:[UIColor customYellowColor]];
        }
        
        _tableHeadLabel = label;
    }
    return _tableHeadLabel;
}

#pragma mark - UITableViewDataSource mothods
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    YWTempTroubleFinishBillCell *cell = [tableView dequeueReusableCellWithIdentifier:YWTempTroubleFinishBillCellIdentifier];
    [self configureTempTroubleBillCell:cell atIndexPath:indexPath];
    return cell;
}

#pragma configure cell
- (void)configureTempTroubleBillCell:(YWTempTroubleFinishBillCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    cell.fd_enforceFrameLayout = NO;
    if (self.troubleMissionModel) {
        cell.troubleMissionModel = self.troubleMissionModel;
    }else if (self.troubleRecord){
        cell.troubleRecord = self.troubleRecord;
    }
    cell.fieldRecordContent = self.fieldRecordContent;
}


#pragma mark - UITableViewDataDelegate mothods
//- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
//{
//    return [tableView fd_heightForCellWithIdentifier:YWTempTroubleFinishBillCellIdentifier cacheByIndexPath:indexPath configuration:^(YWTempTroubleFinishBillCell *cell) {
//        [self configureTempTroubleBillCell:cell atIndexPath:indexPath];
//    }];
//}

@end
